{{define "system/user/add"}}
<html lang="zh">
<head>
    {{template "header" (OssUrl)}}
    <link href="{{OssUrl}}/resource/ajax/libs/select2/select2.min.css" rel="stylesheet"/>
    <link href="{{OssUrl}}/resource/ajax/libs/select2/select2-bootstrap.css" rel="stylesheet"/>
</head>
<body>
<div class="main-content">
    <form id="form-user-add" class="form-horizontal">
        <input name="deptId" type="hidden" id="treeId"/>
        <h4 class="form-header h4">基本信息</h4>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>用户名称：</label>
                    <div class="col-sm-8">
                        <input name="userName" placeholder="请输入用户名称" class="form-control" type="text" maxlength="30"
                               required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>归属部门：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input name="deptName" onclick="selectDeptTree()" id="treeName" type="text"
                                   placeholder="请选择归属部门" class="form-control" required>
                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>手机号码：</label>
                    <div class="col-sm-8">
                        <input id="phonenumber" name="phonenumber" placeholder="请输入手机号码" class="form-control"
                               type="text" maxlength="11" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>邮箱：</label>
                    <div class="col-sm-8">
                        <input id="email" name="email" class="form-control email" type="text" maxlength="50"
                               placeholder="请输入邮箱" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>登录账号：</label>
                    <div class="col-sm-8">
                        <input id="loginName" name="loginName" placeholder="请输入登录账号" class="form-control" type="text"
                               maxlength="30" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label"><span style="color: red; ">*</span>登录密码：</label>
                    <div class="col-sm-8">
                        <input name="password" placeholder="请输入登录密码" class="form-control" type="password"
                               value="" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">用户性别：</label>
                    <div class="col-sm-8">
                        {{getDictTypeSelect "sys_user_sex" "sex" "sex" "form-control m-b" "" "" ""}}
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">用户状态：</label>
                    <div class="col-sm-8">
                        <label class="toggle-switch switch-solid">
                            <input type="checkbox" id="status" checked>
                            <span></span>
                        </label>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">岗位：</label>
                    <div class="col-xs-4">
                        <select id="post" class="form-control select2-multiple" multiple>
                            {{range $index, $post := .posts}}
                                <option value="{{$post.PostId}}"  {{if eq $post.Status "1" }} disabled {{end}}>{{$post.PostName}}</option>
                            {{end}}
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">角色：</label>
                    <div class="col-xs-10">
                        {{range $index, $role := .roles}}
                            <label class="check-box">
                                <input name="role" type="checkbox"
                                       value="{{$role.RoleId}}" {{if eq $role.Status "1" }} disabled {{end}}
                                       text="{{$role.RoleName}}">{{$role.RoleName}}
                            </label>
                        {{end}}
                    </div>
                </div>
            </div>
        </div>
        <h4 class="form-header h4">其他信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">备注：</label>
                    <div class="col-xs-10">
                        <textarea name="remark" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<!--<div class="row">-->
<!--    <div class="col-sm-offset-5 col-sm-10">-->
<!--        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存-->
<!--        </button>&nbsp;-->
<!--        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭-->
<!--        </button>-->
<!--    </div>-->
<!--</div>-->


{{template "footer" (OssUrl)}}
<script src="{{OssUrl}}/resource/ajax/libs/select2/select2.min.js"></script>
<script>
    var prefix = ctx + "/system/user";

    $("#form-user-add").validate({
        onkeyup: false,
        rules: {
            loginName: {
                minlength: 2,
                maxlength: 20,
                remote: {
                    url: prefix + "/profile/checkLoginNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "loginName": function () {
                            return $.common.trim($("#loginName").val());
                        }
                    }
                }
            },
            password: {
                minlength: 5,
                maxlength: 20
            },
            email: {
                email: true,
                remote: {
                    url: prefix + "/profile/checkEmailUniqueAll",
                    type: "post",
                    dataType: "json",
                    data: {
                        "email": function () {
                            return $.common.trim($("#email").val());
                        }
                    }
                }
            },
            phonenumber: {
                isPhone: true,
                remote: {
                    url: prefix + "/profile/checkPhoneUniqueAll",
                    type: "post",
                    dataType: "json",
                    data: {
                        "phonenumber": function () {
                            return $.common.trim($("#phonenumber").val());
                        }
                    }
                }
            },
        },
        messages: {
            "loginName": {
                remote: "用户已经存在"
            },
            "email": {
                remote: "Email已经存在"
            },
            "phonenumber": {
                remote: "手机号码已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = $("#form-user-add").serializeArray();
            var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
            var roleIds = $.form.selectCheckeds("role");
            var postIds = $.form.selectSelects("post");
            data.push({"name": "status", "value": status});
            data.push({"name": "roleIds", "value": roleIds});
            data.push({"name": "postIds", "value": postIds});
            $.operate.save(prefix + "/add", data);
        }
    }

    /*用户管理-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        var url = ctx + "/system/dept/selectDeptTree?deptId=" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            height:"600",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {

        // var tree = layero.find("iframe")[0].contentWindow.$._tree;
        // console.log("body:"+JSON.stringify(body))
        // if ($.tree.notAllowParents(tree)) {
        //     var body = layer.getChildFrame('body', index);
        //     let treeId = body.find('#treeId').val()
        //     let treeName = body.find('#treeName').val()
        //     $("#treeId").val(treeId);
        //     $("#treeName").val(treeName);
        //     console.log("body:"+JSON.stringify(body)+" treeId "+ treeId + "------treeName: "+treeName)
        //     layer.close(index);
        // }

        var body = $.modal.getChildFrame(index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        $.modal.close(index);
    }

    $(function () {
        $('#post').select2({
            placeholder: "请选择岗位",
            allowClear: true
        });
    })
</script>
</body>
</html>
{{end}}